package bai1;

import java.net.*;
import java.io.*;

public class Server {

	public static void main(String[] args) {
		try {

			ServerSocket ss = new ServerSocket(7);
			while (true) {
				try {
					System.out.println("Server dang hoat dong");
					Socket s = ss.accept();
					Process p = new Process(s);
					p.start();
				} catch (IOException e) {
					System.out.println("Connection Error: " + e);
				}
			}

		} catch (IOException e) {
			System.out.println("Create Socket Error: " + e);
		}
	}

}

class Process extends Thread {
	Socket chanel;

	public Process(Socket s) {
		chanel = s;
	}

	public void run() {
		try {

			DataOutputStream os = new DataOutputStream(chanel.getOutputStream());
			BufferedReader is = new BufferedReader(new InputStreamReader(chanel.getInputStream()));
			String str;

			while (true) {

				String n = is.readLine();
				
				if(n.equals("-1")){
					String sr = "quit";
					os.writeBytes(sr);
		            System.out.println("Client da ngat ket noi");
					break;
				}

				switch (n) {
				case "0":
					str = "Khong";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "1":
					str = "Mot";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "2":
					str = "Hai";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "3":
					str = "Ba";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "4":
					str = "Bon";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "5":
					str = "Nam";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "6":
					str = "Sau";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "7":
					str = "Bay";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "8":
					str = "Tam";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;
				case "9":
					str = "Chin";
					os.writeBytes(str);
					os.write(13);
					os.write(10);
					os.flush();
					break;

				default:
					str = "Khong phai la so nguyen";
					os.writeBytes(str);
					break;
				}
			}
		chanel.close();
		} catch (IOException e) {
			System.out.println("Request Processing Error: " + e);
		}
	}
}
